package com.boguan.web.controller.index;

import com.boguan.common.core.domain.AjaxResult;
import com.boguan.common.core.domain.entity.SysUser;
import com.boguan.common.utils.SecurityUtils;
import com.boguan.device.domain.vo.DeviceNumBySite;
import com.boguan.device.service.IDeviceAlarmService;
import com.boguan.device.service.IDeviceService;
import com.boguan.system.service.ISysDeptService;
import com.boguan.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 首页数据展示信息
 *
 * @author chen
 */
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IDeviceAlarmService deviceAlarmService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 根据区域id查询出设备总数，在线数量/离线数量/下级区域数量/下级用户数量
     */
    @ApiOperation(value = "根据区域id查询出设备总数，在线数量/离线数量/下级区域数量/下级用户数量", notes = "区域id不传默认当前登录用户同级区域下的")
    @GetMapping("/device/count")
    public AjaxResult deviceCount(Long deptId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        user.setDeptId(deptId);
        AjaxResult success = AjaxResult.success();
        Integer deviceCount = deviceService.deviceCount(user);
        success.put("deviceCount", deviceCount);
        success.put("online", 0);
        success.put("offline", 0);
        List<Map<String, Integer>> list = deviceService.deviceOnlineCount(user);
        for (Map<String, Integer> map : list) {
            Integer onlineStatus = map.get("onlineStatus");
            if (onlineStatus == 1) {
                //在线
                success.put("online", map.get("number"));
            } else {
                //离线
                success.put("offline", map.get("number"));
            }
        }
        Integer deptCount = sysDeptService.selectDeptCount(user);
        success.put("deptCount", deptCount);
        Integer userCount = sysUserService.selectUserCount(user);
        success.put("userCount", userCount);
        return success;
    }


    /**
     * 根据区域id查询出报警数量，已处理数量，未处理数量
     */
    @ApiOperation(value = "根据区域id查询出报警数量，已处理数量，未处理数量", notes = "区域id不传默认当前登录用户同级区域下的")
    @GetMapping("/alarm/count")
    public AjaxResult alarmCount(Long deptId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        user.setDeptId(deptId);
        Integer alarmCount = deviceAlarmService.selectAlarmCount(user);
        AjaxResult success = AjaxResult.success();
        success.put("alarmCount", alarmCount);
        success.put("untreated", 0);
        success.put("treated", 0);
        List<Map<String, Integer>> list = deviceAlarmService.untreatedAlarmCount(user);
        for (Map<String, Integer> map : list) {
            Integer status = map.get("status");
            if (status == 1) {
                //已处理
                success.put("untreated", map.get("number"));
            } else {
                //未处理
                success.put("treated", map.get("number"));
            }
        }
        return success;
    }

    /**
     * 报警数量趋势
     */
    @ApiOperation(value = "查询出本月的报警数量趋势", notes = "区域id不传默认当前登录用户同级区域下的")
    @GetMapping("/alarm/count/trend")
    public AjaxResult alarmCountTrend(Long deptId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        user.setDeptId(deptId);
        return AjaxResult.success(deviceAlarmService.alarmCountTrend(user));
    }


    /**
     * 根据区域查询出下级区域的设备数量
     */
    @ApiOperation(value = "根据区域查询出下级区域的设备数量", notes = "区域id不传默认当前登录用户同级区域下的")
    @GetMapping("/device/subordinate/count")
    public AjaxResult subordinateDeviceCount(Long deptId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        user.setDeptId(deptId);
        return AjaxResult.success(deviceService.subordinateDeviceCount(user));
    }

    /**
     * 查询设备每种类型的数量
     */
    @ApiOperation("查询设备每种类型的数量")
    @GetMapping("/device/type/count")
    public AjaxResult typeDeviceCount() {
        return AjaxResult.success(deviceService.typeDeviceCount(SecurityUtils.getLoginUser().getUser()));
    }

    /**
     * 查询出报警/预警/严重预警的数量
     */
    @ApiOperation("查询出报警/预警/严重预警的数量")
    @GetMapping("/alarm/level/count")
    public AjaxResult alarmLevelCount() {
        AjaxResult success = AjaxResult.success();
        success.put("early_warning", 0);
        success.put("serious_warning", 0);
        success.put("alarm", 0);
        success.put("fault_alarm", 0);
        List<Map<String, Integer>> list = deviceAlarmService.alarmLevelCount(SecurityUtils.getLoginUser().getUser());
        for (Map<String, Integer> map : list) {
            Integer alarmLevel = map.get("alarmLevel");
            if (alarmLevel == 1) {
                success.put("early_warning", map.get("number"));
            } else if (alarmLevel == 2) {
                success.put("serious_warning", map.get("number"));
            } else if (alarmLevel == 3) {
                success.put("alarm", map.get("number"));
            }else {
                success.put("fault_alarm", map.get("number"));
            }
        }
        return success;
    }


    /**
     * 根据省
     * 市
     * 区
     * 查询出不同的数量
     */
    @ApiOperation(value = "根据省/市/区查询设备信息", notes = "site:位置,type:1省份，2市，3区域,不传为全国")
    @GetMapping("device/number")
    public AjaxResult deviceNumber(DeviceNumBySite deviceNumBySite) {
        return AjaxResult.success(deviceService.selectDeviceNumBySite(deviceNumBySite));
    }


}
